package be.lioche.soupe;

import java.io.File;
import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;

public class Main extends JavaPlugin implements Listener{

	public Event ec;
	public Signs sg;
	public Spawn sp;
	public Commands cm;
	public Shop sh;

	public static String name = "§f[§cLiocheClub§f] ";
	public static HashMap<String, Scoreboard> boards = new HashMap<String, Scoreboard>();


	@Override
	public void onEnable(){
		this.ec = new Event(this);
		this.sg = new Signs(this);
		this.sp = new Spawn(this);
		this.cm = new Commands(this);
		this.sh = new Shop(this);

		PluginManager pm = getServer().getPluginManager();
		pm.registerEvents(ec, this);
		pm.registerEvents(sg, this);
		pm.registerEvents(sp, this);
		pm.registerEvents(cm, this);
		pm.registerEvents(sh, this);
		

		File f = new File("plugins/Soupe/config.yml");

		if(f.exists()){
			System.out.println("[LiocheClub] Fichier de configuration trouvé et chargé !");
			
		}else{
			System.out.println("[LiocheClub] Fichier config.yml non trouvé et généré !");
			getConfig().options().copyDefaults(false);
			}

		getConfig().set("motd", "                                §c❮§fLiocheClub§c❯                                       §f➤ §cPvP-Soupe en développement");
		getConfig().set("version", "Beta v6.0");
		saveConfig();
		Bukkit.broadcastMessage("§a§oReload du plugin PvP-Soupe | " + getConfig().getString("version")+" !"); 
	}

	@Override
	public void onDisable(){
	}

	public final void persoScoreboard(Player p){

		if(!(boards.containsKey(p.getName()))){
			Scoreboard board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
			boards.put(p.getName(), board);
		}

		Scoreboard board = boards.get(p.getName());
		Objective o = board.getObjective("infos");

		if(o == null){		
			o = board.registerNewObjective("infos", "dummy");

			Score coins = o.getScore("Coins:");
			coins.setScore(getConfig().getInt(p.getName()+".Coins"));

			Score killstotaux = o.getScore("Kills totaux:");
			killstotaux.setScore(getConfig().getInt(p.getName()+".TotalKills"));

			Score kills = o.getScore("Kills enchainés:");
			kills.setScore(getConfig().getInt(p.getName()+".Kills"));

			Score morts = o.getScore("Morts:");
			morts.setScore(getConfig().getInt(p.getName()+".Morts"));

			o.setDisplayName("[§cStats§f] §c"+p.getName());
			o.setDisplaySlot(DisplaySlot.SIDEBAR);
			p.setScoreboard(board);
		}else{

			Score coins = o.getScore("Coins:");
			coins.setScore(getConfig().getInt(p.getName()+".Coins"));

			Score killstotaux = o.getScore("Kills totaux:");
			killstotaux.setScore(getConfig().getInt(p.getName()+".TotalKills"));

			Score kills = o.getScore("Kills enchainés:");
			kills.setScore(getConfig().getInt(p.getName()+".Kills"));

			Score morts = o.getScore("Morts:");
			morts.setScore(getConfig().getInt(p.getName()+".Morts"));

			o.setDisplayName("[§cStats§f] §c"+p.getName());
			o.setDisplaySlot(DisplaySlot.SIDEBAR);
			p.setScoreboard(board);
		}


	}

	public final void vieScore(Player p){

		if(!(boards.containsKey(p.getName()))){
			Scoreboard board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
			boards.put(p.getName(), board);
		}

		Scoreboard board = boards.get(p.getName());
		Objective o = board.getObjective("vie");

		if(o == null){		
			o = board.registerNewObjective("vie", "health");

			int pv = (int)p.getHealth();

			Score vie = o.getScore("Vie:");
			vie.setScore(pv/2);

			o.setDisplayName(" demi ♥");
			o.setDisplaySlot(DisplaySlot.BELOW_NAME);
			p.setScoreboard(board);
		}else{
			int pv = (int)p.getHealth();

			Score vie = o.getScore("Vie:");
			vie.setScore(pv/2);

			o.setDisplayName(" demi ♥");
			o.setDisplaySlot(DisplaySlot.BELOW_NAME);
			p.setScoreboard(board);
		}


	}

	public final void repairAll(Player p) {

		if(getConfig().getString(p.getName()+".havekit").equalsIgnoreCase("defaut")){
			if(getConfig().getInt(p.getName()+".Kits.defaut.lvl") == 1){

				ItemStack casque = new ItemStack(Material.IRON_HELMET);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.IRON_BOOTS);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);

				p.updateInventory();
			}else if(getConfig().getInt(p.getName()+".Kits.defaut.lvl") == 2){

				ItemStack casque = new ItemStack(Material.IRON_HELMET);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.IRON_BOOTS);
				
				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();
				
			}else if(getConfig().getInt(p.getName()+".Kits.defaut.lvl") == 3){

				ItemStack casque = new ItemStack(Material.IRON_HELMET);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.IRON_BOOTS);

				casque.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
				plastron.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
				pantalon.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
				bottes.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();
				
			}
		}

		if(getConfig().getString(p.getName()+".havekit").equalsIgnoreCase("flash")){
			if(getConfig().getInt(p.getName()+".Kits.flash.lvl") == 1){

				ItemStack casque = new ItemStack(Material.PUMPKIN);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.IRON_BOOTS);
				casque.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 6);
				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();

			}else if(getConfig().getInt(p.getName()+".Kits.flash.lvl") == 2){

				ItemStack casque = new ItemStack(Material.PUMPKIN);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.IRON_BOOTS);
				bottes.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
				casque.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 6);
				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();

			}else if(getConfig().getInt(p.getName()+".Kits.flash.lvl") == 3){

				ItemStack casque = new ItemStack(Material.PUMPKIN);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.IRON_BOOTS);
				
				bottes.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
				casque.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 6);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();

			}
		}

		if(getConfig().getString(p.getName()+".havekit").equalsIgnoreCase("freezer")){
			if(getConfig().getInt(p.getName()+".Kits.freezer.lvl") == 1){

				ItemStack casque = new ItemStack(Material.IRON_HELMET);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.IRON_BOOTS);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();

			}else if(getConfig().getInt(p.getName()+".Kits.freezer.lvl") == 2){

				ItemStack casque = new ItemStack(Material.IRON_HELMET);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.DIAMOND_BOOTS);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();

			}else if(getConfig().getInt(p.getName()+".Kits.freezer.lvl") == 3){

				ItemStack casque = new ItemStack(Material.IRON_HELMET);
				ItemStack plastron = new ItemStack(Material.IRON_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.IRON_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.DIAMOND_BOOTS);

				bottes.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();
			}
		}

		if(getConfig().getString(p.getName()+".havekit").equalsIgnoreCase("terroriste")){
			if(getConfig().getInt(p.getName()+".Kits.terroriste.lvl") == 1){

				ItemStack casque = new ItemStack(Material.CHAINMAIL_HELMET);
				ItemStack plastron = new ItemStack(Material.CHAINMAIL_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.CHAINMAIL_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.CHAINMAIL_BOOTS);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();

			}else if(getConfig().getInt(p.getName()+".Kits.terroriste.lvl") == 2){

				ItemStack casque = new ItemStack(Material.CHAINMAIL_HELMET);
				ItemStack plastron = new ItemStack(Material.CHAINMAIL_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.CHAINMAIL_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.CHAINMAIL_BOOTS);

				casque.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
				plastron.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
				pantalon.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
				bottes.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();

			}else if(getConfig().getInt(p.getName()+".Kits.terroriste.lvl") == 3){

				ItemStack casque = new ItemStack(Material.CHAINMAIL_HELMET);
				ItemStack plastron = new ItemStack(Material.CHAINMAIL_CHESTPLATE);
				ItemStack pantalon = new ItemStack(Material.CHAINMAIL_LEGGINGS);
				ItemStack bottes = new ItemStack(Material.CHAINMAIL_BOOTS);

				casque.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
				plastron.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
				pantalon.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
				bottes.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);

				p.getInventory().setHelmet(casque);
				p.getInventory().setLeggings(pantalon);
				p.getInventory().setChestplate(plastron);
				p.getInventory().setBoots(bottes);
				p.getItemInHand().setDurability((short) 0);
				p.updateInventory();

			}
		}


	}
}



